package com.loong.musicdemo.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.ui.mine.recent.PlayMvActivity;
import com.loong.musicdemo.util.FormatTimeUtil;

import java.util.List;

/**
 * 用于正常格式显示的MV
 */
public class NormalMVAdapter extends RecyclerView.Adapter<NormalMVAdapter.ViewHolder> {
    private List<MV> mvList;//MV列表
    private RequestOptions requestOptions = new RequestOptions().placeholder(R.drawable.temp);

    public NormalMVAdapter(List<MV> mvList) {
        this.mvList = mvList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_mv, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MV mv = mvList.get(position);//当前MV
        Glide.with(holder.mvRoot).load(mv.getCoverImg()).apply(requestOptions).skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.mvImg);
        holder.mvName.setText(mv.getMvName());
        holder.artistName.setText(mv.getArtistName());
        String briefNumber = FormatTimeUtil.briefNumber(mv.getPlayCount());
        holder.playCount.setText(briefNumber);
        int integer = Integer.parseInt(mv.getDuration());
        String timeToStr = FormatTimeUtil.formatTimeToStr(integer);
        holder.duration.setText(timeToStr);
        holder.mvRoot.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), PlayMvActivity.class);
            intent.putExtra("mvId", mv.getMvID());
            intent.putExtra("mvName", mv.getMvName());
            intent.putExtra("mvPlayCount", mv.getPlayCount());
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mvList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mvRoot;//跟容器
        ImageView mvImg;//MV封面
        TextView playCount;//播放
        TextView duration;//时长
        TextView mvName;//MV 名
        TextView artistName;//作者

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mvRoot = itemView.findViewById(R.id.mv_root);
            mvImg = itemView.findViewById(R.id.mv_img);
            playCount = itemView.findViewById(R.id.play_count);
            duration = itemView.findViewById(R.id.mv_duration);
            mvName = itemView.findViewById(R.id.mv_name);
            artistName = itemView.findViewById(R.id.author_name);
        }
    }

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        super.onViewRecycled(holder);
        ImageView imageView = holder.mvImg;
        if (imageView != null)
            Glide.with(holder.mvImg.getContext()).clear(imageView);
    }
}
